home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / dejagnu.lha / dejagnu-1.0.1 / tcl / tests / upvar.test < prev    next >
Text File  |  1992-11-06  |  5KB  |  198 lines

  1. # Commands covered:  upvar
  2. #
  3. # This file contains a collection of tests for one or more of the Tcl
  4. # built-in commands.  Sourcing this file into Tcl runs the tests and
  5. # generates output for errors.  No output means no errors were found.
  6. #
  7. # Copyright 1991 Regents of the University of California
  8. # Permission to use, copy, modify, and distribute this
  9. # software and its documentation for any purpose and without
  10. # fee is hereby granted, provided that this copyright notice
  11. # appears in all copies.  The University of California makes no
  12. # representations about the suitability of this software for any
  13. # purpose.  It is provided "as is" without express or implied
  14. # warranty.
  15. #
  16. # $Header: /sprite/src/lib/tcl/tests/RCS/upvar.test,v 1.1 91/10/03 16:47:56 ouster Exp $ (Berkeley)
  17.  
  18. if {[string compare test [info procs test]] == 1} then {source defs}
  19.  
  20. test upvar-1.1 {reading variables with upvar} {
  21.     proc p1 {a b} {set c 22; set d 33; p2}
  22.     proc p2 {} {upvar a x1 b x2 c x3 d x4; set a abc; list $x1 $x2 $x3 $x4 $a}
  23.     p1 foo bar
  24. } {foo bar 22 33 abc}
  25. test upvar-1.2 {reading variables with upvar} {
  26.     proc p1 {a b} {set c 22; set d 33; p2}
  27.     proc p2 {} {p3}
  28.     proc p3 {} {upvar 2 a x1 b x2 c x3 d x4; set a abc; list $x1 $x2 $x3 $x4 $a}
  29.     p1 foo bar
  30. } {foo bar 22 33 abc}
  31. test upvar-1.3 {reading variables with upvar} {
  32.     proc p1 {a b} {set c 22; set d 33; p2}
  33.     proc p2 {} {p3}
  34.     proc p3 {} {
  35.     upvar #1 a x1 b x2 c x3 d x4
  36.     set a abc
  37.     list $x1 $x2 $x3 $x4 $a
  38.     }
  39.     p1 foo bar
  40. } {foo bar 22 33 abc}
  41. test upvar-1.4 {reading variables with upvar} {
  42.     set x1 44
  43.     set x2 55
  44.     proc p1 {} {p2}
  45.     proc p2 {} {
  46.     upvar 2 x1 x1 x2 a
  47.     upvar #0 x1 b
  48.     set c $b
  49.     incr b 3
  50.     list $x1 $a $b
  51.     }
  52.     p1
  53. } {47 55 47}
  54.  
  55. test upvar-2.1 {writing variables with upvar} {
  56.     proc p1 {a b} {set c 22; set d 33; p2; list $a $b $c $d}
  57.     proc p2 {} {
  58.     upvar a x1 b x2 c x3 d x4
  59.     set x1 14
  60.     set x4 88
  61.     }
  62.     p1 foo bar
  63. } {14 bar 22 88}
  64. test upvar-2.2 {writing variables with upvar} {
  65.     set x1 44
  66.     set x2 55
  67.     proc p1 {x1 x2} {
  68.     upvar #0 x1 a
  69.     upvar x2 b
  70.     set a $x1
  71.     set b $x2
  72.     }
  73.     p1 newbits morebits
  74.     list $x1 $x2
  75. } {newbits morebits}
  76. test upvar-2.3 {writing variables with upvar} {
  77.     catch {unset x1}
  78.     catch {unset x2}
  79.     proc p1 {x1 x2} {
  80.     upvar #0 x1 a
  81.     upvar x2 b
  82.     set a $x1
  83.     set b $x2
  84.     }
  85.     p1 newbits morebits
  86.     list [catch {set x1} msg] $msg [catch {set x2} msg] $msg
  87. } {0 newbits 0 morebits}
  88.  
  89. test upvar-3.1 {unsetting variables with upvar} {
  90.     proc p1 {a b} {set c 22; set d 33; p2; lsort [info vars]}
  91.     proc p2 {} {
  92.     upvar 1 a x1 d x2
  93.     unset x1 x2
  94.     }
  95.     p1 foo bar
  96. } {b c}
  97. test upvar-3.2 {unsetting variables with upvar} {
  98.     proc p1 {a b} {set c 22; set d 33; p2; lsort [info vars]}
  99.     proc p2 {} {
  100.     upvar 1 a x1 d x2
  101.     unset x1 x2
  102.     set x2 28
  103.     }
  104.     p1 foo bar
  105. } {b c d}
  106. test upvar-3.3 {unsetting variables with upvar} {
  107.     set x1 44
  108.     set x2 55
  109.     proc p1 {} {p2}
  110.     proc p2 {} {
  111.     upvar 2 x1 a
  112.     upvar #0 x2 b
  113.     unset a b
  114.     }
  115.     p1
  116.     list [info exists x1] [info exists x2]
  117. } {0 0}
  118. test upvar-3.4 {unsetting variables with upvar} {
  119.     set x1 44
  120.     set x2 55
  121.     proc p1 {} {
  122.     upvar x1 a x2 b
  123.     unset a b
  124.     set b 118
  125.     }
  126.     p1
  127.     list [info exists x1] [catch {set x2} msg] $msg
  128. } {0 0 118}
  129.  
  130. test upvar-4.1 {nested upvars} {
  131.     set x1 88
  132.     proc p1 {a b} {set c 22; set d 33; p2}
  133.     proc p2 {} {global x1; upvar c x2; p3}
  134.     proc p3 {} {
  135.     upvar x1 a x2 b
  136.     list $a $b
  137.     }
  138.     p1 14 15
  139. } {88 22}
  140. test upvar-4.2 {nested upvars} {
  141.     set x1 88
  142.     proc p1 {a b} {set c 22; set d 33; p2; list $a $b $c $d}
  143.     proc p2 {} {global x1; upvar c x2; p3}
  144.     proc p3 {} {
  145.     upvar x1 a x2 b
  146.     set a foo
  147.     set b bar
  148.     }
  149.     list [p1 14 15] $x1
  150. } {{14 15 bar 33} foo}
  151.  
  152. proc tproc {args} {global x; set x [list $args [uplevel info vars]]}
  153. test upvar-5.1 {traces involving upvars} {
  154.     proc p1 {a b} {set c 22; set d 33; trace var c rw tproc; p2}
  155.     proc p2 {} {upvar c x1; set x1 22}
  156.     set x ---
  157.     p1 foo bar
  158.     set x
  159. } {{x1 {} w} x1}
  160. test upvar-5.2 {traces involving upvars} {
  161.     proc p1 {a b} {set c 22; set d 33; trace var c rw tproc; p2}
  162.     proc p2 {} {upvar c x1; set x1}
  163.     set x ---
  164.     p1 foo bar
  165.     set x
  166. } {{x1 {} r} x1}
  167. test upvar-5.3 {traces involving upvars} {
  168.     proc p1 {a b} {set c 22; set d 33; trace var c rwu tproc; p2}
  169.     proc p2 {} {upvar c x1; unset x1}
  170.     set x ---
  171.     p1 foo bar
  172.     set x
  173. } {{x1 {} u} x1}
  174.  
  175. test upvar-6.1 {errors in upvar command} {
  176.     list [catch upvar msg] $msg
  177. } {1 {wrong # args: should be "upvar ?level? otherVar localVar ?otherVar localVar ...?"}}
  178. test upvar-6.2 {errors in upvar command} {
  179.     list [catch {upvar 1} msg] $msg
  180. } {1 {wrong # args: should be "upvar ?level? otherVar localVar ?otherVar localVar ...?"}}
  181. test upvar-6.3 {errors in upvar command} {
  182.     list [catch {upvar a b} msg] $msg
  183. } {1 {already at top level}}
  184. test upvar-6.4 {errors in upvar command} {
  185.     list [catch {upvar 1 a b} msg] $msg
  186. } {1 {already at top level}}
  187. test upvar-6.5 {errors in upvar command} {
  188.     list [catch {upvar #0 a b} msg] $msg
  189. } {1 {already at top level}}
  190. test upvar-6.6 {errors in upvar command} {
  191.     proc p1 {} {upvar a b c}
  192.     list [catch p1 msg] $msg
  193. } {1 {wrong # args: should be "a ?level? otherVar localVar ?otherVar localVar ...?"}}
  194. test upvar-6.7 {errors in upvar command} {
  195.     proc p1 {} {set a 33; upvar b a}
  196.     list [catch p1 msg] $msg
  197. } {1 {variable "a" already exists}}
  198.